package com.mashibing.juc.c_026_00_interview.A1B2C3;

public class T03_00_cas {

    enum ReadyToRun {T1, T2}

//    static volatile ReadyToRun r = ReadyToRun.T1; // 思考为什么必须volatile
    static ReadyToRun r = ReadyToRun.T1;

    public static void main(String[] args) {

        char[] chars1 = "1234567".toCharArray();
        char[] chars2 = "ABCDEFG".toCharArray();

        new Thread(() -> {
            for (char c : chars1) {
                while (r != ReadyToRun.T1) {
                    // CAS
                }
                System.out.print(c);
                r = ReadyToRun.T2;
            }
        }, "t1").start();

        new Thread(() -> {
            for (char c : chars2) {
                while (r != ReadyToRun.T2) {
                    // CAS
                }
                System.out.print(c);
                r = ReadyToRun.T1;
            }
        }, "t2").start();
    }
}


